<template>
	<view>
		<template v-if="list.length">
			<ScrollView
				class="theme-page-bg"
				:contentHeight="contentHeight"
			>
				<template #content> 
					<view class="m-t-20" v-for="(item, index) in list" :key="index" @click="chooseAddress(item)">
						<AddressItem :dataItem="item" @onEdit="onEdit" @onRadioChange="(e) => onRadioChange(index, e)"></AddressItem>
					</view>
				</template>
			</ScrollView>
		</template>
		<empty v-else :show="false"/>
		<view class="button-bottom padding-20 bg-w flex">
			<view class="button-plain" @click="onExit">退出管理</view>
			<view class="button-style flex-1 m-l-20" @click="toAddAdress">+ 添加收货地址</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import ScrollView from '@/components/accompany/scrollView/scrollView.vue';
	import AddressItem from '../components/adressItem/index.vue';
	import empty from '@/components/empty/index.vue';
	
	import { onLoad, onShow } from '@dcloudio/uni-app';
	import { ref, getCurrentInstance } from 'vue';
	import type { Ref } from 'vue'
	
	const { proxy } = getCurrentInstance() as any;
	
	const list = ref([
		// { name: '张三', mobile: '15666666666', province: '河南省', city: '郑州市', area: ' 管城回族区', detailAddress: '郑东商业中心', defaulted: true },
		// { name: '张三', mobile: '15666666666', province: '河南省', city: '郑州市', area: ' 管城回族区', detailAddress: '郑东商业中心', defaulted: false }
	])
	
	const contentHeight:Ref<number> = ref(0)
	const chooseAddress= (item) =>{
		uni.navigateBack();
		uni.setStorageSync('tmpAddress',item)
		console.log(item,'111')
		console.log(11)
	}
	const getAddressList = () => {
		uni.showLoading({title: '加载中...'})
		const url = proxy.$API.getAddressList;
		proxy.$UNIHTTP.getData(url).then((res: any) => {
			if (res.code == 0) {
				list.value = res.data;
				list.value.forEach(item=>{
					console.log(item,'item')
					if(item.defaulted){
						uni.setStorageSync('address',item)
					}
				})
			} 			
		}).finally(() => {
			uni.hideLoading();
		})
		
	}
	
	const onExit = () => uni.navigateBack();
	
	const toAddAdress = () => {
		uni.navigateTo({url: '/pages/accompany-tmp/my/addressEdit/addressEdit'});
	}
	
	const onEdit = (item) => {
		uni.navigateTo({url: `/pages/accompany-tmp/my/addressEdit/addressEdit?detail=${encodeURIComponent(JSON.stringify(item))}`});
	}
	
	const onRadioChange = (curr: number, e) => {
		const currItem = { ...e };
		currItem.defaulted = !currItem.defaulted;
		uni.showLoading({title: '设置中'});
		const url = proxy.$API.updateAddress;
		proxy.$UNIHTTP.putData(url, currItem).then(res => {
			if(res.code == 0){
				list.value = list.value.map((item, index) => {
					if(curr === index){
						item.defaulted = true;
					}else{
						item.defaulted = false;
					}
					return item;
				})
				uni.setStorageSync('address',currItem)
				return uni.showToast({icon: 'success', title: '设置成功'});
			}
			uni.showToast({icon: 'error', title: '设置失败'});
		}).catch(() => {
			uni.showToast({icon: 'error', title: '设置失败'});
		}).finally(() => {
			uni.hideLoading();
		})
	}
	
	onLoad(() => {
		console.log('onLoad')
		let sysInfo = uni.getStorageSync('sysInfo')
		contentHeight.value = sysInfo.screenHeight - sysInfo.statusBarHeight - 44 - 110/sysInfo.devicePixelRatio
		console.log(contentHeight);
	})
	
	onShow(() => {
		getAddressList();
	})
</script>

<style lang="scss" scoped>
	.theme-page-bg{
		padding-bottom: 122rpx;
	}
	.button-bottom {
		position: fixed;
		height: 100rpx;
		bottom: 0rpx;
		right: 0;
		left: 0;
		view {
			height: 88rpx;
			line-height: 88rpx;
		}
		.button-plain {
			padding: 0 56rpx;
		}
	}
</style>
